package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.erp.Util.TreeNodeBuilder;
import com.erp.Util.WebUtil;
import com.erp.comm.Code;
import com.erp.comm.DataAndView;
import com.erp.comm.TreeNode;
import com.erp.pojo.Permission;
import com.erp.pojo.User;
import com.erp.service.PermissionService;
import com.erp.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private PermissionService permissionService;
    @Autowired RoleService roleService;
    @RequestMapping("loadIndexLeftMenuJson")
    public DataAndView loadIndexLeftMenu() {

        //查当前用户的权限菜单
        //获取当前用户信息
        User user = (User) WebUtil.getSession().getAttribute("user");
        System.out.println(user+"11111");

        //判断用户类型
        List<Permission> list =null;
        if(user.getType()==Code.USER_SUPER){
            //超级用户 查询所有type 为menu的类型
            //设计查询条件
            QueryWrapper<Permission> qw1 = new QueryWrapper<>();
            qw1.eq("type",Code.PERMISSON_MENU);//菜单项
            qw1.eq("available", Code.AVAILABLE);//可用
            list = permissionService.list(qw1);
            System.out.println(list+"bbbbbbbbbbbbbbb");
        }else {
            //
            // 普通用户 uid>rid>pid>permission
            //获取uid
            int uid =user.getId();
            List<Integer> listrids =roleService.getRoleByUid(uid);
            List<Permission> pss =null;//存储permission

            //根据rid查pid
            HashSet<Integer> spids =new HashSet<>();
            for (Integer rid:listrids){
                //每个角色对应的pid集合

                List<Integer> pids=permissionService.getPidsByRid(rid);
                System.out.println(pids);
                spids.addAll(pids);//将集合中的元素存下来
                //根据角色查出来的pid存在重复
                //去重复的pid 再存
            }

//            System.out.println(spids);//在sys_permission中 查type 数据
            QueryWrapper<Permission> qw1 = new QueryWrapper<>();
            qw1.eq("type",Code.PERMISSON_MENU);//菜单项
            qw1.eq("available", Code.AVAILABLE);//可用
//            System.out.println(listrids);
            qw1.in("id",spids);//指定id范围查询

            list=permissionService.list(qw1);

            System.out.println(spids);

        }
        //定义空的节点集合
        List<TreeNode> treeNodes =new ArrayList<TreeNode>();
        //遍历当前用户的persission集合
        for (Permission p :list){

            //id
            Integer id = p.getId();
            Integer pid = p.getPid();
            String title = p.getTitle();
            String icon =p.getIcon();
            String href= p.getHref();
            Boolean spread =(p.getOpen() == 1)?true:false;
            TreeNode node =new TreeNode( id,  title,  href,  spread,  icon,  pid);
            treeNodes.add(node);
        }
        System.out.println(treeNodes+"ccccccccccccccccccccc");

        //构建层级关系
        List<TreeNode> list1= TreeNodeBuilder.Builder(treeNodes ,1);
        System.out.println(list1+"ddddddddddddddd");
         return new DataAndView(list1);



    }
}
